package OilSimNoteBook;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Container;         // Container object is a component of awt that will contain other AWT components
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;

/**
 *
 * @author anthony
 */
public class App extends OilSimUIComponent implements Observer, ActionListener {

    private JPanel mainPanel;
    JMenuBar menuBar;
    JMenu fileMenu;
    JMenuItem saveMenuItem, loadMenuItem;
    CardLayout layout;
    ArrayList<OilSimUIComponent> components;
    private File recentOpenDir;

    public App() {
        menuBar = new JMenuBar();

        //Build the first menu.
        fileMenu = new JMenu("File");
        fileMenu.setMnemonic(KeyEvent.VK_F);

        saveMenuItem = new JMenuItem("Save", KeyEvent.VK_S);
        saveMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
        saveMenuItem.getAccessibleContext().setAccessibleDescription("Save file...");

        loadMenuItem = new JMenuItem("Load", KeyEvent.VK_L);
        loadMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, ActionEvent.CTRL_MASK));
        loadMenuItem.getAccessibleContext().setAccessibleDescription("Load file...");

        fileMenu.add(saveMenuItem);
        fileMenu.add(loadMenuItem);

        menuBar.add(fileMenu);
        mainPanel = new JPanel();
        //layout = new CardLayout();
        //mainPanel.setLayout(layout);

        components = new ArrayList<OilSimUIComponent>();
        init();
    }

    private void init() {
        //addComp(new Grid());
        addComp(new IntroPane());
        loadMenuItem.addActionListener(this);
        saveMenuItem.addActionListener(this);

        //saveMenuItem.setEnabled(false);
        //loadMenuItem.setEnabled(false);
    }

    public void addComp(OilSimUIComponent comp) {
        components.add(comp);
        comp.addObserver(this);
        getFrame().setContentPane(comp.getPanel());
        getFrame().getContentPane().validate();
    }

    @Override
    public Container getPanel() {
        return getFrame().getContentPane();
    }

    public Grid getGrid() {
        for (OilSimUIComponent c : components) {
            if (c.getClass().equals(Grid.class)) {
                return (Grid) c;
            }
        }
        return null;
    }

    @Override
    public JFrame getFrame() {
        JFrame f = super.getFrame();
        f.setJMenuBar(menuBar);
        f.pack();
        return f;
    }

    @Override  // action for save command 
    public void actionPerformed(ActionEvent e) {
        Grid grid = getGrid();
        final JFileChooser fc = new JFileChooser();
        fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        URL loc = this.getClass().getProtectionDomain().getCodeSource().getLocation();
        
        
        fc.setCurrentDirectory(recentOpenDir != null? recentOpenDir: new File(loc.getFile()));
        
        File selected = null;
        
        if (e.getActionCommand().equals(saveMenuItem.getActionCommand())) {
            if (fc.showSaveDialog(getFrame()) == JFileChooser.APPROVE_OPTION) {
                selected = fc.getSelectedFile();
                grid.saveToFile(selected);
            }
        } else if (e.getActionCommand().equals(loadMenuItem.getActionCommand())) {
            
            if (fc.showOpenDialog(getFrame()) == JFileChooser.APPROVE_OPTION) {
                selected = fc.getSelectedFile();
                grid.loadFromFile(selected);
            }
        }
        
        if(selected != null){
            recentOpenDir = selected;
        }
    }

    @Override
    public void update(Observable o, Object arg) {
        saveMenuItem.setEnabled(false);
        loadMenuItem.setEnabled(false);

        if (arg.equals(IntroPane.START_CLICKED)) {
            addComp(new LoginPane());
        } else if (arg.equals(LoginPane.LOGIN_CLICKED)) {
            addComp(new Grid());
            saveMenuItem.setEnabled(true);
            loadMenuItem.setEnabled(true);
        }
    }

    public static void main(String[] args) {
        try {
            // Set System L&F
            UIManager.setLookAndFeel(
                    UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            Logger.getLogger("MAIN").log(Level.WARNING, "Unable to set look & feel...");
        }

        new App().showFrame();
    }
}
